package com.i2bt.app.files;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.UserConfig;

import com.i2bt.R;
import com.i2bt.adapter.FilesUploadListAdapter;
import com.i2bt.app.BaseActivity;
import com.i2bt.app.files.model.AndroidLocalFilesReader;
import com.i2bt.app.files.model.FilesUploadJsonHandle;
import com.i2bt.bean.FileBean;
import com.i2bt.bean.FileInfoBean;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 
 * Title : 上传文件活动类
 *
 * Description : 读取sd卡里面的文件, 提供选择并上传到连接的homebox
 *
 * CreatedTime : 2014-9-1
 *
 * @author wuliao & kakaxi & yi
 *
 * @version 1.0
 *
 */

public class FilesUploadActivity extends BaseActivity implements OnItemClickListener {
	
	private AndroidLocalFilesReader localFilesReader; // 本地文件读取器
	
	private FilesUploadJsonHandle filesUploadJsonHandle; // 上传文件json处理器
	
	private ListView filesUploadListView; // 文件列表视图
	
	private List<FileInfoBean> filesUploadData; // 文件列表数据
	
	private FilesUploadListAdapter uploadListAdapter; // 文件列表适配器
	
	private TextView uploadText; // 文件父路径定位文本
	
	private Button uploadBtn; // 上传按钮
	
	@Override
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_files_upload);
		
		logic();
		initView();
	}
	
	@Override
	protected void initView() {
		// 初始化本地文件列表视图
		uploadListAdapter = new FilesUploadListAdapter(this, filesUploadData);
		filesUploadListView = (ListView) findViewById(R.id.files_upload_list);
		filesUploadListView.setAdapter(uploadListAdapter);
		filesUploadListView.setOnItemClickListener(this);
		
		// 初始化路径定位文本
		uploadText = (TextView) findViewById(R.id.files_upload_text);
		uploadText.setText(localFilesReader.getCurrentPath());
		
		// 初始化上传按钮
		uploadBtn = (Button) findViewById(R.id.files_upload_btn);
		uploadBtn.setOnClickListener(this);
	}

	@Override
	protected void logic() {
		// 初始化处理器和本地文件读取器
		if (filesUploadJsonHandle == null) {
			filesUploadJsonHandle = new FilesUploadJsonHandle(this);
		}
		localFilesReader = new AndroidLocalFilesReader();
		// 读取本地根目录的内容
		filesUploadData = localFilesReader.enterSDCardFiles("");
	}
	
	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
		// 进入下一层, 如果是文件夹就往下一层走
		if (!filesUploadData.get(position).isFile()) {
			// 进入对应名字的文件夹
			String dirName = filesUploadData.get(position).getFileName();
			filesUploadData = localFilesReader.enterSDCardFiles(dirName);
			// 获取目前地址并设置到定位文本中
			uploadText.setText(localFilesReader.getCurrentPath());
			// 显示列表数据
			uploadListAdapter.setData(filesUploadData);
		}

	}

	@Override
	public void onClick(View v) {
		filesUploadRequest();
	}
	
	/**
	 * 上传文件请求
	 */
	private void filesUploadRequest() {
		
		// 获取选中的文件位置列表
		List<Integer> checkList = uploadListAdapter.getCheckedList();
		// 新建将要上传文件的列表信息
		String[] uploadList = new String[checkList.size()];
		// 是否可以上传
		boolean canUpload = true;
		for (int i = 0; i < checkList.size(); i++) {
			
			// 如果选中的文件中包含文件夹将停止上传
			if (!filesUploadData.get(checkList.get(i)).isFile()) {
				canUpload = false;
				break;
			}
			
			// 对上传列表添加上传文件信息
			try {
				uploadList[i] = URLEncoder.encode(
						filesUploadData.get(checkList.get(i)).getFileName(), "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		// 不可上传
		if (!canUpload) {
			toastHandler("暂不支持上传文件夹");
			return;
		}
		
		// 发送上传请求
		FileBean fileBean = new FileBean();
		fileBean.setAct(AndroidRestContract.UPLOAD_FILES);
		fileBean.setUploadFiles(uploadList);
		fileBean.setMD5("abcdnimageb");
		fileBean.setFileSize("1");
		fileBean.setParentPath(UserConfig.currentFilesPath);
		
		filesUploadJsonHandle.postTo(AndroidRestContract.REST_FILES, fileBean);
	}
	
	/**
	 * 文件上传响应
	 */
	public void filesUploadResponse() {
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (!localFilesReader.currentPath.equals("")) {
				// 退到上一层
				filesUploadData = localFilesReader.backSDCardFiles();
				uploadListAdapter.setData(filesUploadData);
				// 设置文件定位文本
				uploadText.setText(localFilesReader.getCurrentPath());
			}
			else {
				// 不能再退 就把活动关闭
				finish();
			}
		}
		
		return true;
	}
	
	@Override
	public void backHomeOperate() {
		finish();
	}

}
